<?php
defined('OA_LIB_PATH') || define('OA_LIB_PATH', '/opt/op/OALib');
defined('OPEN_LIB_PATH') || define('OPEN_LIB_PATH', '/opt/op/OpenLib');

require_once 'OaLogic.php';
require_once OPEN_LIB_PATH . '/open/models/OaAppModel.php';
require_once OPEN_LIB_PATH . '/open/models/OaEmailModel.php';
require_once OPEN_LIB_PATH . '/open/models/OaCompanyModel.php';
require_once OPEN_LIB_PATH . '/open/business-logic/OaSyncInfoLogic.php';
require_once OPEN_LIB_PATH . '/open/business-logic/service/OaServiceHelper.php';

class OaEmailLogic extends OaLogic
{
    public static function send_mail($check_point, $appId, $spId, $subject = '', $content = '')
    {
		//定义步骤
		$mailMod = new OaEmailModel();
		$appMod = new OaAppModel();
		$spMod = new OaCompanyModel();
		
		$app = $appMod->getApp( $appId );
		$sp = $spMod->getCompany( $spId );
		
		$mails = $mailMod->getSendEmailList(array('check_point' => $check_point, 'delete_flag' => 0));
		
		if(!empty($mails) && is_array($mails))
		{
			foreach($mails as $mail_config)
			{
				if(empty($subject)) 
					$subject = $mail_config['subject'];
				if(empty($content)) 
					$content = $mail_config['content'];
				
				$subject = OaEmailLogic::replace_content($subject,$app, $sp);
				$content = OaEmailLogic::replace_content($content,$app, $sp);
				
				$toAddressList = explode(";",$mail_config['receiver_email']);
    			$mailHost = $GLOBALS['gConfig']['mail_host'];
    			$mailWebSender = $GLOBALS['gConfig']['mail_web_sender'];
    			
			    $phpmailerlite = new PHPMailer();
			    try {
				  $phpmailerlite->CharSet = "UTF-8";
				  $phpmailerlite->IsSMTP();
				  $phpmailerlite->SMTPAuth = false;
				  $phpmailerlite->Host = $mailHost;
				  $phpmailerlite->SetFrom($mailWebSender, "openIII", false);
				  $phpmailerlite->AddReplyTo("no-reply@snda.com", "no-reply");
				  $phpmailerlite->IsHTML(true);
				  foreach($toAddressList as $to) {
				  	  
				  	  if(empty($to))
				  	  	  continue;
				  	  
				  	  if($to == '[APPOWNER]')
				  	  {
				  	  	  if($sp['legal_person_email'])
				  	  	  	  $to = $sp['legal_person_email'];
				  	  	  else
				  	  	  	  continue;
				  	  }
				  	  
				  	  if($to == '[TECMANAGER]')
				  	  {
				  	  	  if(isset($app['ext_info_for_qq']['tecManEmail']) && !empty($app['ext_info_for_qq']['tecManEmail']))
				  	  	  	  $to = $app['ext_info_for_qq']['tecManEmail'];
				  	  	  else
				  	  	  	  continue;
				  	  }
				  	  
				  	  if($to == '[CUSMANAGER]')
				  	  {
				  	  	  if(isset($app['ext_info_for_qq']['proManEmail']) && !empty($app['ext_info_for_qq']['proManEmail']))
				  	  	  	  $to = $app['ext_info_for_qq']['proManEmail'];
				  	  	  else
				  	  	  	  continue;
				  	  }
				  	  if(!preg_match("/@/",$to))
				  	  {
				  	  	  $to .= '@snda.com';
				  	  }
				  	  
				      $phpmailerlite->AddAddress($to);    
				  }	   	  
				  $phpmailerlite->Subject = $subject;
				  $phpmailerlite->Body = $content;
				  $phpmailerlite->Send();
				}
				catch (phpmailerException $e) {
			//	  echo $e->getMessage();
				} catch (Exception $e) {
			//	   echo $e->getMessage();
			    }
				
			}
		}
		return true;
    }
    
    public static function replace_content($text,$app, $sp)
    {
    	if(!empty($sp))
    	{
    		$text = preg_replace("/\[SPID\]/i", $sp['id'] ,$text);
    		$text = preg_replace("/\[SPNAME\]/i", $sp['name'] ,$text);
    	}
    	if(!empty($app))
    	{
    		$text = preg_replace("/\[APPID\]/i", $app['id'] ,$text);
    		$text = preg_replace("/\[APPNAME\]/i", $app['name'] ,$text);
    	}
    	return $text;
    }
}